﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Department.aspx.cs" Inherits="MagicBag.CRM.Web.Admin.Department" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>部门管理</title>
    <style type="text/css">
        .DivCenter
        {
            margin-left: auto;
            margin-right: auto;
        }
        #SearchBar
        {
            margin-bottom: 5px;
        }
        
        #fm
        {
            margin: 0;
            padding: 10px 30px;
        }
        .ftitle
        {
            font-size: 14px;
            font-weight: bold;
            color: #666;
            padding: 5px 0;
            margin-bottom: 10px;
            border-bottom: 1px solid #ccc;
        }
        .fitem
        {
            margin-bottom: 5px;
        }
        .fitem label
        {
            display: inline-block;
            width: 80px;
        }
    </style>
    <link href="../Script/jquery-easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../Script/jquery-easyui/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../Script/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="../Script/jquery-easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#tt').treegrid({
                nowrap: false,
                rownumbers: true,
                animate: true,
                collapsible: true,
                url: 'MagicBag.CRM.Service/DepartmentService.crm?act=GetDepartments',
                idField: 'ID',
                treeField: 'CName', //显示树字段，图标显示在此字段
                //				frozenColumns: [[
                //	                { title: '编号', field: 'ID', width: 200,
                //	                    formatter: function (value) {
                //	                        return '<span style="color:red">' + value + '</span>';
                //	                    }
                //	                }
                //				]],
                columns: [[
                //                    { field: 'ID', title: '编号', width: 150 },
					{field: 'CName', title: '中文名称' },
					{ field: 'EName', title: '英文名列', width: 220 },
					{ field: 'Remark', title: '备注', width: 150 }
				]],
                onClickRow: function (row) {
                    // alert(row.ID);
                },
                onContextMenu: function (e, row) {
                    e.preventDefault();
                    $(this).treegrid('unselectAll');
                    $(this).treegrid('select', row.ID);
                    $('#mm').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                }
            });
        });


        function qq(value, name) {
            // alert(value + ":" + name)
            if (name == '所有')
                name = '';
            var queryParams = $('#tt').datagrid('options').queryParams;
            queryParams.Category = name;
            queryParams.KeyWords = value;
            $('#tt').datagrid('options').queryParams = queryParams;
            $('#tt').datagrid('reload');
        }


        var url;
        function newDept() {
            $('#dlg').dialog('open').dialog('setTitle', '新增');
            $('#fm').form('clear');
            url = 'MagicBag.CRM.Service/DepartmentService.crm?act=Insert';

           

            $('#cc').combobox({
                url: 'MagicBag.CRM.Service/DepartmentService.crm?act=GetDepartments&ddl=1',
                valueField: 'ID',
                textField: 'CName'
            });

            var row = $('#tt').treegrid('getSelected');
            if (row)
                $("#cc").combobox("setValue",row.ID);
        }
        function editDept() {
            var row = $('#tt').treegrid('getSelected');
            if (row) {
                $('#dlg').dialog('open').dialog('setTitle', '修改');

                // $('#fm input[name="Category"]').combobox({

                $('#cc').combobox({
                    url: 'MagicBag.CRM.Service/DepartmentService.crm?act=GetDepartments&ddl=1',
                    valueField: 'ID',
                    textField: 'CName'
                });


                $('#fm').form('load', row);
                url = 'MagicBag.CRM.Service/DepartmentService.crm?act=Update';
            }
        }
        function saveDept() {
            $('#fm').form('submit', {
                url: url,
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.Success) {
                        $('#dlg').dialog('close'); 	// close the dialog
                        $('#tt').treegrid('reload'); // reload the user data
                    } else {
                        $.messager.alert("错误", result.Msg, "error");
                    }
                }
            });
        }
        function removeDept() {
            // var row = $('#tt').datagrid('getSelected');
            var row = $('#tt').treegrid('getSelected');
            var rows = $.merge($('#tt').treegrid('getChildren', row.ID),[row]);//合并当前结点及子结点
            //return;
            if (rows.length > 0) {
                $.messager.confirm('确认', '您确定要删除此部门吗？其所有子部门将全部删除！', function (r) {
                    if (r) {
                        $(rows).each(function () {
                            $.post('MagicBag.CRM.Service/DepartmentService.crm?act=Delete', { id: $(this)[0].ID }, function (result) {
                                if (result.Success) {
                                    $('#tt').treegrid('reload'); // reload the user data
                                } else {
                                    $.messager.show({	// show error message
                                        title: 'Error',
                                        msg: result.Msg
                                    });
                                }
                            }, 'json');

                        });

                    }
                });
            }


        }

    </script>
</head>
<body>
    <div class="DivCenter" style="width: 100%; height: auto;">
        <!--搜索条-->
        <!--明细-->
        <table id="tt"  toolbar="#toolbar">
        </table>

        <!--工具条-->
        <div id="toolbar">
            <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newDept()">
                新增</a> <a href="#" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editDept()">
                    修改</a> <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="removeDept()">
                        删除</a>
        </div>

        <!--右键菜单-->
        <div id="mm" class="easyui-menu" style="width: 120px;">
            <div onclick="newDept()">
                新增</div>
            <div onclick="editDept()">
                修改</div>
            <div onclick="removeDept()">
                删除</div>
        </div>
        <!--对话框-->
        <div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px"
            closed="true" buttons="#dlg-buttons">
            <div class="ftitle">
                部门信息</div>
            <form id="fm" method="post" novalidate>
            <div class="fitem">
                <input name="ID" type="hidden" class="easyui-validatebox" readonly="readonly"/>
            </div>
            <div class="fitem">
                <label>
                    中文名称:</label>
                <input name="CName"  class="easyui-validatebox" required="true"/>
                <%--<select id="cc" name="Category">
                <option></option>
                </select>--%>
            </div>
            <div class="fitem">
                <label>
                    英文名称:</label>
                <input name="EName" class="easyui-validatebox"/>
            </div>
            <div class="fitem">
                <label>
                    所属部门:</label>
                <input id="cc" name="ParentID" class="easyui-combobox" style="width: 150px;" />
            </div>
            <div class="fitem">
                <label>
                    备注:</label>
                <input name="Remark"/>
            </div>
            </form>
        </div>
        <div id="dlg-buttons">
            <a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveDept()">保存</a>
            <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">
                取消</a>
        </div>
    </div>
</body>
</html>
